import {
    ADD_TODO,
    //   ADD_TODO_ASYNC,
    TOGGLE_COMPLETE,
    DEL_TODO,
    CHANGE_DISPLAY
} from '../actions/todo'
// import {combineReducers} from 'redux';
// import counter from './counter';
// import even from './even';

// 合并Reducer，合成一个state树
// export default combineReducers({counter, even});

// 定义默认状态
const initState = {
    display: 'all', // 显示状态(all,uncompleted,completed)
    todos: []
}

const reducer = (state = initState, action) => {
    let newState
    switch (action.type) {
        case ADD_TODO:
            newState = {
                todos: [...state.todos, action.payload]
            }
            break
        case TOGGLE_COMPLETE:
            newState = {
                todos: state.todos.map(item => {
                    if (item.id === action.payload) {
                        item.isComplete = !item.isComplete
                    }
                    return item
                })
            }
            break
        case DEL_TODO:
            newState = {
                todos: state.todos.filter(todo => todo.id !== action.payload)
            }
            break
        case CHANGE_DISPLAY:
            newState = {
                display: action.payload,
                todos: [...state.todos]
            }
            break
        default:
            newState = state
            break
    }
    return newState
}
export default reducer
